// pages/selectShop/selectShop.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    page: 1,
    pageSize: 10,
    isLoading: false,
    noMoreData: false,
    isRefreshing: false,
    items: [],
    currentShop: null,
    selectShop: null,
    showShopDialog: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.queryMyMdmMerchantStoreList();
  },

  /**
   * 获取门店列表
   */
  queryMyMdmMerchantStoreList: function (refresh = false) {
    var that = this;
    if (this.data.isLoading) return;

    this.setData({
      isLoading: true
    });
    const {
      page,
      pageSize
    } = this.data;
    if (!refresh) {
      wx.showLoading({
        title: '加载中',
      })
    }
    let params = {
      page: page,
      limit: pageSize
    }
    getApp().api.queryMyMdmMerchantStoreList(params, {
      success: function (res) {
        if (res.data.rows) {
          let items = res.data.rows
          items = refresh ? items : that.data.items.concat(items),
            items.forEach(e => {
              e.check = false
            })
          that.setData({
            items: items,
            showShopDialog: items.length > 0 ? true : false,
            noMoreData: res.data.total > items.length ? false : true,
            isRefreshing: false,
            isLoading: false
          })
          wx.hideLoading()
          that.checkShopItem()
        }
      },
      fail: function (err) {
        that.checkShopItem()
        that.setData({
          isLoading: false
        });
        wx.hideLoading()
      }
    });
  },

  loadMore() {
    if (this.data.noMoreData || this.data.isRefreshing) return;
    this.setData({
      page: this.data.page + 1
    })
    this.queryMyMdmMerchantStoreList();
  },

  refresh() {
    this.setData({
      isRefreshing: true,
      noMoreData: false,
      page: 1
    });
    this.queryMyMdmMerchantStoreList(true);
  },


  checkShop: function (e) {
    let index = e.currentTarget.dataset.index
    let {
      items,
      selectShop
    } = this.data
    items.forEach((e, i) => {
      if (i == index) {
        e.check = true
        selectShop = e
      } else {
        e.check = false
      }
    })
    this.setData({
      selectShop,
      items
    })
  },


  confirmDialog: function () {
    //TODO 去执行添加
    let {
      selectShop,
      items
    } = this.data
    let hasCheck = false
    items.forEach(e => {
      if (e.check) {
        hasCheck = true
        selectShop = e
      }
    })
    if (hasCheck) {
      this.setData({
        items,
        currentShop: selectShop
      })
      getApp().saveMerchantStoreInfo(selectShop)
      wx.switchTab({
        url: '/pages/deviceList/deviceList',
      })
    }
    this.setData({
      showDialog: false
    })
  },


  checkShopItem: function () {
    let {
      items
    } = this.data
    if (items.length == 0) { //没有绑定门店
      wx.switchTab({
        url: '/pages/deviceList/deviceList',
      })
      this.setData({
        showDialog: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})